package com.codebuffer.nowcoder;


import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

public class IsSymmetrical {

    static boolean isSymmetrical(TreeNode pRoot) {
        Queue<TreeNode> queue = new LinkedBlockingQueue<>();
        if (null!=pRoot)
        queue.add(pRoot);
        while (queue.size()>0){
            int size = queue.size();
            Object[] objects = queue.toArray();
            for (int i = 1; i <= size; i++) {
                TreeNode poll = queue.poll();
                if(poll.val != Integer.MAX_VALUE){
                    queue.add(null!=poll.left?poll.left:new TreeNode(Integer.MAX_VALUE));
                    queue.add(null!=poll.right?poll.right:new TreeNode(Integer.MAX_VALUE));
                }
                int val = ((TreeNode) objects[size - i]).val;
                if(i<=size/2){
                    if(poll.val != val){
                        return false;
                    }
                }

            }
        }
        return true;
    }

    public static void main(String[] args) {

        TreeNode treeNode1 = new TreeNode(5);
        TreeNode treeNode2 = new TreeNode(5);
        TreeNode treeNode3 = new TreeNode(5);
        TreeNode treeNode4 = new TreeNode(5);
        TreeNode treeNode8 = new TreeNode(5);
//        TreeNode treeNode11 = new TreeNode(5);
        TreeNode treeNode7 = new TreeNode(5);
        treeNode1.left = treeNode2;
        treeNode1.right = treeNode3;
        treeNode2.left = treeNode4;
        treeNode2.right = null;
        treeNode3.left = null;
        treeNode3.right = treeNode7;
        treeNode4.left = treeNode8;
        treeNode4.right = null;

        treeNode7.left = null;
//        treeNode7.right = treeNode11;
        System.out.println(isSymmetrical(treeNode1));
    }
}
